<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
    	// Returns the search results from the notifications table
    	function getNotifications() {
			$db = new Connect();

			$sql = "SELECT nID, pName, Posting.pID, nDatetime, subject, isRead FROM Notice " .
				   "INNER JOIN Posting ON Posting.pID = Notice.pID " .
				   "WHERE Notice.mID = " . $_SESSION["mid"] .
				   " ORDER BY nDatetime DESC";
			
			$result = $db->query($sql);
			
			if(!$result || mysql_num_rows($result) == 0)
			{
				return;
			}
			else
			{
				$rows = $db->to_array($result);
				return $rows;
			}
    	}
    ?>
    <div id="main">
    	<p class="title">
    		Inbox
    	</p>
<?php
	$rows = getNotifications();
	
	if (!isset($rows))
	{
		print "No messages found.";
	}
	else
	{
		print "<table border=\"0\">";
		print "<col width=\"30px\">";
		print "<col width=\"160px\">";
		print "<col width=\"160px\">";
		print "<col width=\"210px\">";

		print "<tr>";
		print "<td></td>";
		print "<th class=\"normal\">Origin</th>";
		print "<th class=\"normal\">Subject</th>";
		print "<th class=\"normal\">Date</th>";
		print "</tr>";

		$rowcount = count($rows);
		for ($i = 0; $i < $rowcount; $i++)
		{
			$class = 'legal';
			if (!$rows[$i]['isRead'])
				$class = 'legal-bold';
			
			print "<tr>";
			print "<td><a href=\"notification.php?nid=" . $rows[$i]['nID'] . "\"><img src=\"./img/letter.png\" height=\"30px\" width=\"30px\" alt=\"Letter\"/></a></td>";
			print "<td class=\"" . $class . "\"><a href=\"posting.php?pid=" . $rows[$i]['pID'] . "\">" . $rows[$i]['pName'] . "</a></td>";
			print "<td class=\"" . $class . "\"><a href=\"notification.php?nid=" . $rows[$i]['nID'] . "\">" . $rows[$i]['subject'] . "</a></td>";
			print "<td class=\"" . $class . "\">" . $rows[$i]['nDatetime'] . "</td>";
			print "</tr>";
		}
		
		print "</table>";
	}
?>
    </div>
</body>
</html>